/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package modeloprediccion;

import java.util.ArrayList;
import motorprediccion.Parada;

/**
 *
 * @author dani
 */
public class ParametrosParada {
    private Parada p;
    private Double tiempoBase, k;
    private ArrayList<Double> erroresBase;
    private ArrayList<Double> erroresPrediccion;
    
    public ParametrosParada(Parada p, Double tb)
    {
        this.p = p;
        this.tiempoBase = tb;
        erroresBase = new ArrayList<Double>(0);
        erroresPrediccion = new ArrayList<Double>();
        this.k = new Double(0.5);
    }

    public Parada getP() {
        return p;
    }

    public Double getTiempoBase() {
        return tiempoBase;
    }

    public Double getPrediccion()
    {
        if(erroresPrediccion.size()>0)
            return tiempoBase + k*erroresPrediccion.get(erroresPrediccion.size()-1);
        else
            return tiempoBase;
    }

    private void actualizarK() {
        float pt = 0;
        float pb = 0;

        for(int i=0; i < erroresPrediccion.size(); i++)
        {
            pt += erroresPrediccion.get(i)*erroresPrediccion.get(i);
        }
        pt = pt/erroresPrediccion.size();

        for(int i=0; i < erroresBase.size(); i++)
        {
            pb += erroresBase.get(i)*erroresBase.get(i);
        }
        pb = pb/erroresBase.size();

        this.k = 1 / (1 + (pb*pt/k));
    }

    void actualizarParametros(Double errorBase, Double errorPrediccion)
    {
        int capacidad = 5;

        erroresBase.add(errorBase);
        erroresPrediccion.add(errorPrediccion);

        if(erroresBase.size()>capacidad)
        {
            erroresBase.remove(0);
            erroresPrediccion.remove(0);
        }

        actualizarK();
    }

}
